﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tau.Model;
using Tau.Service;
using Tau.ViewModels;
using Tau.Web.Infrastructure;

namespace Tau.Web.Areas.Admin.Controllers
{
    public class PageAdminController : BaseAdminController
    {
        public ActionResult Index()
        {
            return View("Index", new PageList { Pages = GetAllPages() });
        }

        public ActionResult ShowAllPages()
        {
            return PartialView("ManagePages", new PageList { Pages = GetAllPages() });
        }

        private List<Page> GetAllPages()
        {
            var svc = new TauService();

            return svc.GetAllPages();
        }

        public void UpdateParentOrder(string parentOrder)
        {
            if (parentOrder.Length > 0 && parentOrder.Length > 0)
            {
                var parentOrders = parentOrder.Split('_').ToList();

                var svc = new TauService();

                foreach (var orderedParent in parentOrders)
                {
                    if (orderedParent.Trim().Length > 0)
                    {
                        var pageIdAndOrder = orderedParent.Split('-');

                        int pageId = 0;
                        int pageOrder = 0;

                        if (int.TryParse(pageIdAndOrder[0], out pageId) &&
                            int.TryParse(pageIdAndOrder[1], out pageOrder))
                        {
                            svc.UpdatePageOrder(pageId, pageOrder);
                        }
                    }
                }
            }
        }

        public void UpdatePagePositions(string parentIds, string pageIds)
        {
            if (parentIds.Length > 0 && pageIds.Length > 0)
            {
                var parentIdStrings = parentIds.Split('_').ToList();
                var childIdListOfArrays = pageIds.Split('-').ToList();

                var svc = new TauService();

                int index = 0;
                foreach (var parentId in parentIdStrings)
                {
                    if (parentId.Trim().Length > 0)
                    {
                        var childIdStrings = childIdListOfArrays[index].Split('_').ToList();
                        if (childIdStrings.Count > 0)
                        {
                            var childIds = new List<int>();
                            foreach(string childIdString in childIdStrings)
                            {
                                if (childIdString.Trim().Length > 0)
                                {
                                   childIds.Add(Convert.ToInt32(childIdString)); 
                                }
                            }

                            if (childIds.Count > 0)
                            {
                                svc.UpdatePageHierarchy(Convert.ToInt32(parentId), childIds);
                            }
                        }
                    }
                    index++;
                }
            }
        }

        public ActionResult AddEditPage(int? pageId)
        {
            var svc = new TauService();
            var images = svc.GetAllImages();

            Page editingPage = pageId.HasValue && pageId > 0 ? svc.GetPage(pageId.Value) : new Page();

            return PartialView("AddEditPage", new AddEditPage
                                                  {
                                                      EditingPage = editingPage,
                                                      Images= new ImageList { Images = images, Mode = DisplayMode.View}
                                                  } );
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddEditPage(AddEditPage addEditPage)
        {
            string pageText = Request["pageText"];
            var svc = new TauService();

            if (addEditPage.EditingPage.PageId > 0)
            {
                addEditPage.EditingPage.PageCode = pageText;
                svc.UpdatePage(addEditPage.EditingPage);
            }
            else
            {
                Page newPage = new Page()
                {
                    OrderId = 0,
                    PageCode = pageText,
                    PageName = addEditPage.EditingPage.PageName,
                    PageTitle = addEditPage.EditingPage.PageTitle
                };
                svc.AddPage(newPage);
            }
            
            return RedirectToAction("Index");
        }

        public ActionResult AddParent()
        {
            return PartialView("AddParent", new AddEditPage(){EditingPage = new Page()});
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddParent(AddEditPage addEditPage)
        {
            Page newPage = new Page()
            {
                OrderId = 0,
                PageName = addEditPage.EditingPage.PageTitle,
                PageTitle = addEditPage.EditingPage.PageTitle
            };
            var svc = new TauService();
            svc.AddParentPage(newPage);

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int pageId)
        {
            var svc = new TauService();
            svc.DeletePage(pageId);
            return RedirectToAction("Index");
        }

        [CacheControl(HttpCacheability.NoCache), HttpGet]
        public ActionResult GetSortedList(string sortBy)
        {
            PageList pageList = null;

            Infrastructure.Session.SetSortBy(sortBy);

            var svc = new TauService();

            pageList = svc.GetSortedPageList(sortBy, Infrastructure.Session.SortDescending);

            return PartialView("PageList", pageList);
        }
    }
}
